const getAssociatedVariable = dataset => {
  let { variableConfig, mergeVariableConfig, globalVariableId } = dataset
  let variableList = [], result = []
  if (mergeVariableConfig && mergeVariableConfig instanceof Array) {

  } else {
    mergeVariableConfig = []
  }

  try {
    let variable = JSON.parse(variableConfig)
    if (variable && variable instanceof Array) {
      variableList.push(...variable)
    }
  } catch (e) {
    console.error('变量信息转换异常: ', e)
  }
  if(mergeVariableConfig && mergeVariableConfig.length) {
    mergeVariableConfig.forEach(item => {
      if (!variableList.find(v => v.name === item.name)) {
        variableList.push(item)
      }
    })
  }
  // console.log(variableList)
  return variableList
}

/**
 * 处理表格数据 转换对象数据
 * @param {*} tableList 列表数据
 * @param {*} type 类型 none 将对象去除(空字符串) json 将对象转换为JSON数据
 * @returns
 */
const handleTableData = (tableList, type = 'none') => {
  if (tableList && tableList instanceof Array) {
    if (tableList.length) {
      let cell = tableList[0]
      let need = Object.keys(cell).reduce((r, c) => {
        if (cell[c] instanceof Array || cell[c] instanceof Object) {
          r.push(c)
        }
        return r
      }, [])

      return tableList.reduce((r, c) => {
        let newObj = { ...c }
        need.forEach(key => {
          if (type === 'none') newObj[key] = ''
          if (type === 'json') newObj[key] = JSON.stringify(c[key])
        })
        r.push(newObj)
        return r
      }, [])
    } else {
      return tableList
    }

  } else {
    return tableList
  }
}

export {
  getAssociatedVariable,
  handleTableData
}